﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BOHVote.BLL;
 
using BOHVote.Common;
using BOHVote.Entity;

namespace BOH
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChooceLoginType();
                BindInfo();
                BindDoc();
               
            }
        }

        #region <<  读取登陆方式  >>
        public void ChooceLoginType()
        {
            bool isShow = SysConfigClass.IsShowPeronsLogin();
            radioPersonLogin.Visible = isShow;
        }
        #endregion

        #region <<  登陆  >>
        protected void butOK_Click(object sender, EventArgs e)
        {
            BHUser bu ;
            string strUserID = this.txtUserID.Text.Trim();
            string strPassword = txtPassword.Text.Trim();//FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(),"MD5");
            string LoginTypeSelected = string.Empty;
            bool isExist = false;
            if (radioPersonLogin.Checked)
            {
                LoginTypeSelected = "P";
                isExist = UserClass.HasUserCode(strUserID);
            }
            else
            {
                LoginTypeSelected = "O";
            }
            if ( LoginTypeSelected == "O")
            {
                bu = UserClass.getUser(strUserID, strPassword, LoginTypeSelected);
                if (bu != null)
                {
                    labError.Text = "";
                    Session["user"] = bu;
                    switch (bu.RightType.ToUpper())
                    {

                        case "Q":
                        case "S":
                            Response.Redirect("Interview/InterView.aspx");
                            break;
                        default:
                            Response.Redirect("Information/Information.aspx");
                            break;
                    } 
                }
                else
                {
                    labError.Text = "用户名/密码错误,请重新输入!";
                }
            }
            else if (LoginTypeSelected == "P")
            {
                if (!isExist)
                {
                    bu = new BHUser();
                    labError.Text = "";
                    bu.UserCode = txtUserID.Text.Trim();
                    bu.UserType = "N";
                    Session["user"] = bu;
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>if(confirm('您的身份证号码不存在,需要注册个人信息')){window.location='ControlPanel/controlPanel.aspx';}</script>");
                    //Response.Redirect("ControlPanel/controlPanel.aspx", true);
                }
                else
                {
                    bu = UserClass.getUser(strUserID, strPassword, LoginTypeSelected);
                    if (bu != null)
                    {
                        labError.Text = "";
                        Session["user"] = bu;
                        Response.Redirect("ControlPanel/controlPanel.aspx");
                    }
                    else
                    {
                        labError.Text = "用户名/密码错误,请重新输入!";
                    }
                }
            }
            else
            {
                labError.Text = "用户名/密码错误,请重新输入!";
            }
        }
        #endregion

        #region <<  相关信息  >>
        public void BindInfo()
        {
            DataSet dst = NotifyClass.getAllVerifiedNotify();
            repInfo.DataSource = dst;
            repInfo.DataBind();

        }

        #endregion

        #region <<  文档下载  >>
        public void BindDoc()
        {
            DataSet dst = NotifyClass.getAllVerifiedNotifyFile();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                dr["Notify_Context"] = common.getAppSetting("FolderforLoadPath") + "/File/" + dr["Notify_Context"].ToString();
            }
            repDoc.DataSource = dst;
            repDoc.DataBind();
        }
        #endregion
    }
}
